/*
 * Copyright (c) 2012 Jonathan Tyers
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.org.uplink.configproxy;

import java.lang.reflect.Method;

/**
 * Represents a method on a {@code @}{@link ConfigProxy} interface annotated with {@code @}{@link ConfigValue}, and provides easy access to the
 * annotations on it.
 * <p>
 * Unlike {@link Method}, this class is <b>not</b> final, allowing us to easily mock it.
 * 
 * @author jonny
 */
public class MethodInfo {
	/**
	 * The class in which the method resides.
	 */
	private final String declaringClassName;

	/**
	 * The method name.
	 */
	private final String name;

	/**
	 * The {@link ConfigValue} on the method.
	 */
	private final ConfigValue configValue;

	/**
	 * The method return type.
	 */
	private final Class<?> returnType;

	public MethodInfo(final String declaringClassName, final String name, final ConfigValue configValue,
			final Class<?> returnType) {
		super();
		this.declaringClassName = declaringClassName;
		this.name = name;
		this.configValue = configValue;
		this.returnType = returnType;
	}

	/**
	 * @return the full canonical name of the class in which the method resides
	 */
	public String getDeclaringClassName() {
		return declaringClassName;
	}

	/**
	 * @return the method name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the {@code @}{@link ConfigValue} annotation on the method.
	 */
	public ConfigValue getConfigValue() {
		return configValue;
	}

	/**
	 * @return the method return type.
	 */
	public Class<?> getReturnType() {
		return returnType;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((configValue == null) ? 0 : configValue.hashCode());
		result = prime * result + ((declaringClassName == null) ? 0 : declaringClassName.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((returnType == null) ? 0 : returnType.hashCode());
		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		MethodInfo other = (MethodInfo) obj;
		if (configValue == null) {
			if (other.configValue != null) {
				return false;
			}
		} else if (!configValue.equals(other.configValue)) {
			return false;
		}
		if (declaringClassName == null) {
			if (other.declaringClassName != null) {
				return false;
			}
		} else if (!declaringClassName.equals(other.declaringClassName)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (returnType == null) {
			if (other.returnType != null) {
				return false;
			}
		} else if (!returnType.equals(other.returnType)) {
			return false;
		}
		return true;
	}
}
